﻿using Microsoft.AspNetCore.Mvc;
using ProjectScheduling_web.dao;
using ProjectScheduling_web.model;

namespace ProjectScheduling_web.Controllers.U
{
    public class ContractController : BController
    {
        private static readonly ContractDao @base = ContractDao.Base;
        [HttpGet]
        public IActionResult Search(int type, string key, string start, string end, int page = 1, int row = 15)
        {
            var st = DateTime.TryParse(start, out DateTime _st) ? _st : DateTime.MinValue;
            var ed = DateTime.TryParse(end, out DateTime _ed) ? _ed : DateTime.MaxValue;
            var data = @base.FindAll(page, row,
                it =>
                (type == 0 || it.TypeId == type) &&
                (string.IsNullOrEmpty(key) || (it.Desc != null && it.Desc.Contains(key)) || (it.ProjectName != null && it.ProjectName.Contains(key))) &&
                (string.IsNullOrEmpty(start) || it.SignTime >= st) &&
                (string.IsNullOrEmpty(end) || it.SignTime <= ed)
                );
            return Success(data);
        }
        [HttpPost]
        public IActionResult Add([FromBody] Contract req)
        {
            if (req == null) return Fail("数据失败");
            if (string.IsNullOrEmpty(req.Code = req.Code?.Trim())) return Fail("合同编号不能为空");
            if (@base.Exists(it => it.Code == req.Code)) return Fail("合同编号已存在，请检查");
            req.CreateTime = DateTime.Now;
            if (req.TypeId == 0) return Fail("请选择合同类型");
            var type = DictionaryDataDao.Base.Find(it => it.Category == model.Enums.EnumDictionaryCategory.合同类型 && it.Value == req.TypeId);
            if (type == null) return Fail("合同类型不存在，请检查");
            req.TypeName = type.Name;
            if (req.OrgId == 0) return Fail("请选择部门");
            var org = DictionaryDataDao.Base.Find(it => it.Category == model.Enums.EnumDictionaryCategory.部门 && it.Value == req.OrgId);
            if (org == null) return Fail("部门不存在，请检查");
            req.OrgName = org.Name;
            if (CurrUser != null)
            {
                req.CreaterId = CurrUser.Id;
                req.CreaterName = CurrUser.Name;
            }
            var id = @base.Add(req);
            return id > 0 ? Success("创建成功") : Fail("创建失败");
        }
        [HttpPost]
        public IActionResult Update([FromBody] Contract req)
        {
            if (req == null) return Fail("数据失败");
            if (req.TypeId == 0) return Fail("请选择合同类型");
            if (string.IsNullOrEmpty(req.Code = req.Code?.Trim())) return Fail("合同编号不能为空");
            if (@base.Exists(it => it.Code == req.Code && it.Id != req.Id)) return Fail("合同编号已存在，请检查");
            var type = DictionaryDataDao.Base.Find(it => it.Category == model.Enums.EnumDictionaryCategory.合同类型 && it.Value == req.TypeId);
            if (type == null) return Fail("合同类型不存在，请检查");
            req.TypeName = type.Name;
            if (req.OrgId == 0) return Fail("请选择部门");
            var org = DictionaryDataDao.Base.Find(it => it.Category == model.Enums.EnumDictionaryCategory.部门 && it.Value == req.OrgId);
            if (org == null) return Fail("部门不存在，请检查");
            req.OrgName = org.Name;
            var id = @base.Update(req, it => new { it.ProjectName, it.Desc, it.PayCondition, it.Money, it.TypeId, it.SignTime, it.SalerId, it.SalerName, it.OrgId, it.OrgName, it.TypeName, it.CustomerId, it.CustomerName, it.Code });
            return id > 0 ? Success("修改成功") : Fail("修改失败");
        }
        [HttpGet]
        public IActionResult Remove(int id)
        {
            var ct = @base.Delete(id);
            return ct > 0 ? Success("删除成功") : Fail("删除失败");
        }
    }
}
